using System;
using System.Collections.Generic;
using System.Linq;
using UploadClient.Interfaces;
using UploadClient.Interfaces.Upload;
using UploadClient.View;
using UploadClient.ViewModel;
using Microsoft.Win32;

namespace UploadClient.Framework
{
    public class DefaultCommunication : ICommunication
    {

        public DefaultCommunication(Options options, ConnectionSettings conSettings)
        {
            Options = options;
            ConSettings = conSettings;
        }

        #region ICommunication Members

        public string GetSelecteFile()
        {
            string result = "";

            OpenFileDialog ofd = new OpenFileDialog { Multiselect = false };
            if (ofd.ShowDialog() ?? false)
                result = ofd.FileName;

            return result;
        }

        public IEnumerable<string> GetSelectedFiles()
        {
            List<string> result = new List<string>();

            OpenFileDialog ofd = new OpenFileDialog { Multiselect = true };
            if (ofd.ShowDialog() ?? false)
                result.AddRange(ofd.FileNames);

            return result;
        }

        public void ShowWindow(ViewModelBase vm)
        {
            if (vm is ConfigurationViewModel)
            {
                var settings = new SettingsWindowView();
                var viewmodel = new SettingsWindowViewModel(vm.Communication);

                settings.DataContext = viewmodel;
                viewmodel.RequestClose += delegate { settings.Close(); };
                settings.Show();
            }
        }

        public IUploadProvider GetUploadProvider(Guid guid)
        {
            UploadManager um = new UploadManager(Options);

            // ToDo: obsolet? 
            // provider.Settings = this.ConSettings.SelectedConnection;

            return um.GetUploadPlugins().FirstOrDefault(up => up.GetConfiguration().FirstOrDefault().Guid.Equals(guid));
        }


        public ConnectionSettings ConSettings { get; private set; }

        public Options Options { get; private set; }

        #endregion
    }
}
